#include "glu_grad_jvp_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>
#include <vector>
using namespace ge;
namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
  GluGradJvpTilingData tiling;
  auto glu_out_shape = context->GetInputShape(0)->GetStorageShape();
    int dim = *context->GetAttrs()->GetInt(0);
    if(dim < 0) dim += glu_out_shape.GetDimNum();
    {
        uint32_t repeat = 1;
        uint32_t interval = 1;
        for(int i=0; i<dim; i++)
        {
            repeat *= glu_out_shape.GetDim(i);
        }
        for(int i=dim; i<glu_out_shape.GetDimNum(); i++)
        {
            interval *= glu_out_shape.GetDim(i);
        }
        interval /= 2;
        uint32_t BLOCK_DIM = repeat;
        if(BLOCK_DIM > 40)
            BLOCK_DIM = 40;
        uint32_t bigNum = repeat % BLOCK_DIM;
        uint32_t bigLength = repeat / BLOCK_DIM;
        uint32_t smallLength = repeat / BLOCK_DIM;
        if(bigNum != 0) bigLength ++;
        tiling.set_bigNum(bigNum);
        tiling.set_bigLength(bigLength);
        tiling.set_smallLength(smallLength);
        tiling.set_interval(interval);
        if(context->GetInputTensor(0)->GetDataType() == DataType::DT_FLOAT)
        {
            if(interval < 1728)
            {
                context->SetTilingKey(1);
            }
            else
            {
                context->SetTilingKey(2);
            }
        }
        else
        {
            context->SetTilingKey(3);
        }
        context->SetBlockDim(BLOCK_DIM);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    }
  return ge::GRAPH_SUCCESS;
}
}
namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
{
const auto inputDataType = context->GetInputDataType(0);
context->SetOutputDataType(0, inputDataType);
return ge::GRAPH_SUCCESS;
}
}
namespace ops {
class GluGradJvp : public OpDef {
public:
    explicit GluGradJvp(const char* name) : OpDef(name)
    {
        this->Input("x_grad")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_BF16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("y_grad")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_BF16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_BF16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("v_y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_BF16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("v_x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_BF16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("jvp_out")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_BF16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("dim").AttrType(OPTIONAL).Int(-1);
        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);
        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");
    }
};
OP_ADD(GluGradJvp);
}
